#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <ctime>

#include "MSTCOSSDK.h"
#include "Fatworm.h"
#include "Commander.h"
#pragma comment(lib,"MSTCOSSDK.lib")
using namespace std;

Commander& DFDer = Commander::getInstance();

bool meet = false;

void Iteration(OSInterface* const system){
//	cout << clock()/CLOCKS_PER_SEC << endl;
	vector<ResourceInfo> islands = system->Resource;
	vector<ShipInfo> ships = system->Ship;
// 	for (int i=0; i<islands.size(); ++i)
// 		cout << islands[i].ID << " ";
// 	cout << endl;
// 	for (int i=0; i<ships.size(); ++i)
// 		cout << ships[i].ID << " ";
// 	cout << endl;

	DFDer.setTeam(system->Faction);

	DFDer.beforeUpdate();
	for (unsigned int i=0; i<ships.size(); ++i){
		if (ships[i].Faction==system->Faction) {
			DFDer.updateOurs(ships[i]);
		} else
			DFDer.updateEnemies(ships[i]);
	}
	DFDer.updateIslands(islands);
	DFDer.afterUpdate();

 	DFDer.makeOrder();
	if (!meet) {
		DFDer.ours[2].curCommand = -1;
		DFDer.ours[2].moveToPoint(TPoint(1000,0));
		meet = true;
	}

 	DFDer.regularCheck();

 	DFDer.act(system);

// 	if (!meet) {
// 		system->MoveTo(ships[0].ID, 500, -500);
// 		system->MoveTo(ships[1].ID, 500, -450);
// 	}
// 	if (ships[0].PositionX > 0) {
// 		system->StartRotatingTo(ships[0].ID, -500, -500);
// 		system->MoveTo(ships[1].ID, -500, -450);
// 		meet = true;
// 	}


#ifdef CONTROL
	char c = 'x';
	float x, y;
	if (!DFDer.ours[0].isMoving)
		cin >> c >> x >> y;
	switch (c) {
	case 'm':
		system->MoveTo(DFDer.ours[0].ID, x, y);
		break;
	case 'a':
		system->Attack(DFDer.ours[0].ID, int(x));
		break;
	case 'r':
		system->StartRotating(DFDer.ours[0].ID, x);
		break;
	}
#endif
}

int main(int argc, char* argv[])
{
// 	freopen("log.txt", "w", stdout);
	char* temp[2];
	temp[0] = "brother2gole";
	temp[1] = "255.255.255";
	srand(0);
	MSTCOSSDK::StartGame(2, temp, Iteration);
	return 0;
}
